﻿using MusicRipperDataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Timers;
using System.Windows.Forms;

namespace UWSP_Music_Ripper
{
    class RipperApi
    {
        ManagementScope scope;
        ManagementEventWatcher w = null;
        WqlEventQuery q;
        CD_Reader.CD_Reader myCD;
        Album myAlbum;
        System.Timers.Timer myTimer;
        public delegate void TimerCallback(String s);
        public delegate void RipDoneCallback(String s);
        List<TimerCallback> timerCallbacks;
        List<RipDoneCallback> ripDoneCallbacks;
        string sourceDir;

        public String SourceDir
        {
            get { return sourceDir; }
            set { sourceDir = value; }
        }
        string destinationDir;

        public String DestinationDir
        {
            get { return destinationDir; }
            set { destinationDir = value; }
        }

        public RipperApi()
            : base()
        {
            timerCallbacks = new List<TimerCallback>();
            ripDoneCallbacks = new List<RipDoneCallback>();
            this.myAlbumHandler = new AlbumHandler();
            SetUpTimer();
        }
        public void registerTimerCallback(TimerCallback t)
        {
            timerCallbacks.Add(t);

        }
        public void registerRipDoneCallback(RipDoneCallback r)
        {
            ripDoneCallbacks.Add(r);

        }
        public Album MyAlbum
        {
            get { return myAlbum; }
            set { myAlbum = value; }
        }
        AlbumHandler myAlbumHandler;

        public CD_Reader.CD_Reader MyCD
        {
            get { return myCD; }
            set { myCD = value; }
        }

        public void Dispose()
        {
            w.Dispose();
        }

        public delegate void eventArrivedCallback(object sender, EventArrivedEventArgs e);
        public void StartMonitoringCD(eventArrivedCallback c)
        {
            scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;

            q = new WqlEventQuery();
            q.EventClassName = "__InstanceModificationEvent";
            q.WithinInterval = new TimeSpan(0, 0, 15);
            // DriveType - 5: CDROM
            q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5";
            w = new ManagementEventWatcher(scope, q);
            // register async. event handler
            w.EventArrived += new EventArrivedEventHandler(c);
            w.Start();
        }

        public void rip(List<string> TracksToRip)
        {
            this.myCD.SetTracksToRip(TracksToRip);
            this.myAlbum.SetTracks(TracksToRip);
            this.myCD.RipAllTracks();
            myTimer.Start();

        }
        public List<string> GetAlbumTitles()
        {
            return myAlbumHandler.GetAlbumTitles();
        }
        public bool DoesThisAlbumExist(String title, String artist)
        {
            Album a = new Album(title, artist);
            return myAlbumHandler.DoesThisAlbumExist(a);   
        }
        public string GetRipProgress()
        {
            return myCD.GetRipProgress();
        }
        private void SetUpTimer()
        {
            myTimer = new System.Timers.Timer();
            myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
            myTimer.Interval = 1000;
        }
        private void myTimer_Elapsed(object source, ElapsedEventArgs e)
        {
           
            foreach (TimerCallback t in timerCallbacks)
            {
                t(GetRipProgress());
            }
            string s = myCD.GetRipState();
            switch (s)
            {
                case "Done":
                    myTimer.Stop();
                    foreach (RipDoneCallback r in ripDoneCallbacks)
                    {
                        r(s);
                    }
                    FileHelper fh = new FileHelper();
                    fh.MoveFiles(sourceDir, destinationDir, myAlbum);
                    //    JewelCaseHelper.WriteTheCase(this.myAlbum);
                    this.myAlbumHandler.AddToAlbums(this.myAlbum);
                    this.myAlbumHandler.WriteToFile();
                    myAlbum.Dispose();
                    break;
            }
        }


    }
}
